package com.wwwarehouse.contract.contract.info;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.PayMentItemBean;
import com.wwwarehouse.contract.contract.TheParentFragment;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/11/3.
 * 信息卡 支付项
 */

public class InfoCardPayItemFragment extends TheParentFragment {
	private TextView mWeekPayTv; //周付
	private TextView mMonthPayTv; //月付
	private TextView mAliPayTv; //支付宝
	private TextView mWechatPayTv; //微信
	private TextView mBankNameTv; //银行卡姓名
	private TextView mBankWhereTv; //银行卡开户行
	private TextView mBankNumTv; //银行卡账号
	private TextView mPayOrderTv; //支持下单付
	private TextView mOfflinePayTv; //支持线下付

	//一些支付项Layout 需要隐藏的
	private RelativeLayout mOrderPayRl;
	private RelativeLayout mOfflinePayRl;
	private RelativeLayout mPayWeekRl;
	private RelativeLayout mPayMonthRl;
	private RelativeLayout mAlipayRl;
	private RelativeLayout mWechatRl;
	private RelativeLayout mBankNameRl;
	private RelativeLayout mBankWhereRl;
	private RelativeLayout mBankNumRl;
	private String mContractUkid;
	private int REQUEST_PAYMENT_ITEM_INFO_CODE = 1;
	@Override
	protected int getContentId() {
		return R.layout.info_card_pay_item_fragment;
	}

	@Override
	public void setTitle() {
		mActivity.setTitle(R.string.contract_modify_info_card_payment_item_title);
	}

	@Override
	protected void init(View view) {
		mPayOrderTv = (TextView) view.findViewById(R.id.pay_order_tv);
		mOfflinePayTv = (TextView) view.findViewById(R.id.offline_pay_tv);
		mWeekPayTv = (TextView) view.findViewById(R.id.pey_week_tv);
		mMonthPayTv = (TextView) view.findViewById(R.id.pay_month_tv);
		mAliPayTv = (TextView) view.findViewById(R.id.alipay_tv);
		mWechatPayTv = (TextView) view.findViewById(R.id.wechat_tv);
		mBankNameTv = (TextView) view.findViewById(R.id.bank_name_tv);
		mBankWhereTv = (TextView) view.findViewById(R.id.bank_where_tv);
		mBankNumTv = (TextView) view.findViewById(R.id.bank_num_tv);
		mOrderPayRl = (RelativeLayout) view.findViewById(R.id.order_pay_layout);
		mOfflinePayRl = (RelativeLayout) view.findViewById(R.id.offline_layout);
		mPayWeekRl = (RelativeLayout) view.findViewById(R.id.pay_week_layout);
		mPayMonthRl = (RelativeLayout) view.findViewById(R.id.pay_month_layout);
		mAlipayRl = (RelativeLayout) view.findViewById(R.id.alipay_layout);
		mWechatRl = (RelativeLayout) view.findViewById(R.id.wechat_layout);
		mBankNameRl = (RelativeLayout) view.findViewById(R.id.bank_name_layout);
		mBankWhereRl = (RelativeLayout) view.findViewById(R.id.bank_where_layout);
		mBankNumRl = (RelativeLayout) view.findViewById(R.id.bank_code_layout);

	}

	@Override
	public void requestDatas() {
		if (getArguments() == null || StringUtils.isNullString(getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID))) {
			mLoadingView.showSystemView(true);
			mLoadingView.setSystemListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					requestDatas();
				}
			});
			return;
		}
		mContractUkid = getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID);
		Map<String, String> mParamsMap = new HashMap<>();
		mParamsMap.put("contractUkid", mContractUkid);
		httpPost(ContractConstant.MODIFY_GET_PAYMENT_ITEM_METHOD ,mParamsMap ,REQUEST_PAYMENT_ITEM_INFO_CODE );


	}

	@Override
	public void onSuccess(CommonClass commonClass, int what) {
		if (commonClass == null || commonClass.getData() == null || commonClass.getCode() == null) {
			return;
		}
		if (what == REQUEST_PAYMENT_ITEM_INFO_CODE) {
			List<PayMentItemBean.DataBean> mPaymentList = JSON.parseArray(commonClass.getData().toString(), PayMentItemBean.DataBean.class);
			if (mPaymentList == null || mPaymentList.size() == 0) {
				mLoadingView.showEmptyView(true);
				mLoadingView.setEmptyListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						requestDatas();
					}
				});
				return;
			}
			if (StringUtils.isNoneNullString(mPaymentList.get(0).getPayOrderFlag()) && mPaymentList.get(0).getPayOrderFlag().equals("1")) {
				mPayOrderTv.setVisibility(View.VISIBLE);
			} else {
				mPayOrderTv.setVisibility(View.GONE);
			}
			if (StringUtils.isNoneNullString(mPaymentList.get(0).getPayOfflineFlag()) && mPaymentList.get(0).getPayOfflineFlag().equals("1")) {
				mOfflinePayTv.setVisibility(View.VISIBLE);
			} else {
				mOfflinePayTv.setVisibility(View.GONE);
			}
			if (StringUtils.isNoneNullString(mPaymentList.get(0).getPayWeek())) {
				if (mPaymentList.get(0).getPayWeek().equals("0")) {
					mPayWeekRl.setVisibility(View.GONE);
				} else {
					mPayWeekRl.setVisibility(View.VISIBLE);
					String num = StringUtils.numToChinese(mPaymentList.get(0).getPayWeek());
					mWeekPayTv.setText(StringUtils.getResourceStr(mActivity, R.string.contract_info_card_pay_week_text, num));
				}
			} else {
				mPayWeekRl.setVisibility(View.GONE);
			}
			if (StringUtils.isNoneNullString(mPaymentList.get(0).getPayMonth())) {
				if (mPaymentList.get(0).getPayMonth().equals("0")) {
					mPayMonthRl.setVisibility(View.GONE);
				} else {
					mPayMonthRl.setVisibility(View.VISIBLE);
					mMonthPayTv.setText(StringUtils.getResourceStr(mActivity, R.string.contract_info_card_pay_month_text, mPaymentList.get(0).getPayMonth()));
				}

			} else {
				mPayMonthRl.setVisibility(View.GONE);
			}
			if (StringUtils.isNoneNullString(mPaymentList.get(0).getPayOfflineFlag())) {
				if (mPaymentList.get(0).getPayOfflineFlag().equals("1")) {
					mOfflinePayRl.setVisibility(View.VISIBLE);
				} else {
					mOfflinePayRl.setVisibility(View.GONE);
				}
			}
			if (StringUtils.isNoneNullString(mPaymentList.get(0).getPayOrderFlag())) {
				if (mPaymentList.get(0).getPayOrderFlag().equals("1")) {
					mOrderPayRl.setVisibility(View.VISIBLE);
				} else {
					mOrderPayRl.setVisibility(View.GONE);
				}
			}
			if (StringUtils.isNoneNullString(mPaymentList.get(0).getAccountAlipay())) {
				mAlipayRl.setVisibility(View.VISIBLE);
				mAliPayTv.setText(mPaymentList.get(0).getAccountAlipay());
			} else {
				mAlipayRl.setVisibility(View.GONE);
			}
			if (StringUtils.isNoneNullString(mPaymentList.get(0).getAccountWechat())) {
				mWechatRl.setVisibility(View.VISIBLE);
				mWechatPayTv.setText(mPaymentList.get(0).getAccountWechat());
			} else {
				mWechatRl.setVisibility(View.GONE);
			}
			if (StringUtils.isNoneNullString(mPaymentList.get(0).getAccountBankCardName())) {
				mBankNameRl.setVisibility(View.VISIBLE);
				mBankNameTv.setText(mPaymentList.get(0).getAccountBankCardName());
			} else {
				mBankNameRl.setVisibility(View.GONE);
			}
			if (StringUtils.isNoneNullString(mPaymentList.get(0).getAccountBank())) {
				mBankWhereRl.setVisibility(View.VISIBLE);
				mBankWhereTv.setText(mPaymentList.get(0).getAccountBank());
			} else {
				mBankWhereRl.setVisibility(View.GONE);
			}
			if (StringUtils.isNoneNullString(mPaymentList.get(0).getAccountBankCardCode())) {
				mBankNumRl.setVisibility(View.VISIBLE);
				mBankNumTv.setText(mPaymentList.get(0).getAccountBankCardCode());
			} else {
				mBankNumRl.setVisibility(View.GONE);
			}


		}
	}

	@Override
	public void onReLoad(int code) {

	}
}
